<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="类型名称">
                <a-input v-model="queryParam.name" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="handleSearch()">查询</a-button>
                <a-button style="margin-left: 8px" @click="reset()">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
      </div>

      <a-table ref="table" size="default" rowKey="id" :columns="columns" :dataSource="loadData" :alert="false"
        :pagination="pagination" @change="handleTableChange">

        <span slot="describ" slot-scope="text">
          <ellipsis :length="4" tooltip>{{ text }}</ellipsis>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a @click="handleDelete(record)">删除</a>
          </template>
        </span>
      </a-table>

      <create-form ref="createModal" :loading="confirmLoading" @ok="handleOk" />
    </a-card>
  </page-header-wrapper>
</template>
<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import CreateForm from './modules/CreateForm'
import { Modal } from 'ant-design-vue';
import api from '@/api/baike/type'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id'
  },
  {
    title: '名称',
    dataIndex: 'name',
  },
  {
    title: '描述',
    dataIndex: 'describ',
    scopedSlots: { customRender: 'describ' }
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    CreateForm,
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 查询参数
      queryParam: {
        name: null,
        pageNo: 0,
        pageSize: 10
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showTotal: total => `共 ${total} 条数据`,
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: [],
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created() {
    this.getList()
  },
  computed: {
  },
  methods: {
    //  获取列表
    getList() {
      this.loading = true
      api.getList(this.queryParam).then(res => {
        this.loading = false
        this.loadData = res.data.dataList

        this.pagination.total = res.data.totalCount
        this.pagination.current = this.queryParam.pageNo + 1
      }).catch(err => {
        console.log(err);
        this.loading = false
      })
    },
    //  查询
    handleSearch() {
      this.queryParam.pageNo = 0
      this.getList()
    },
    //  重置
    reset () {
      this.queryParam =  {
        name: null,
        pageNo: 0,
        pageSize: 10
      }
      this.getList()
    },
    handleAdd() {
      this.$refs.createModal.show()
    },
    handleEdit(record) {
      this.$refs.createModal.show(record)
    },
    handleTableChange(e) {
      this.queryParam.pageNo = e.current - 1
      this.getList()
    },
    handleOk() {
      this.getList()
    },
    handleCancel() {
      this.visible = false
    },
    handleDelete(record) {
      // this.$message.info(`${record.id} 删除成功`)
      Modal.confirm({
        title: '提示',
        content: '确认删除？',
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          console.log('成功');
          api.delete({ id: record.id }).then(res => {
            this.$message.success(`删除成功`)
            this.getList()
          }).catch(err => {
            this.$message.error(`删除失败`)
          })
        }
      });
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}

</script>